local SPELL_ICON = {}

SpellData = {}

function SPELL_ICON:Init()
	self.Name = "Test"
	self.Range = 240
	self.Tooltip = "Poontang"
	self.CoolDownProg = 0
	self.CoolDown = 1
	self.GCDProg = 0
	self.GCD = 0.5
	self.DoingGCD = false
	self.SpellID = 0
	self.IconScale = 1
	self.Key = KEY_Q
	self.NextPress = CurTime()
	self.Color = Color(255, 255, 255, 255)
	self.CastWhileCasting = true
	self.Swapped = false
	self.OnCooldown = false
	self.InSpellbook = false
	self.Draggable = false
	self.DropSpot = false
	self.Selected = false
	self.RequiresTarget = true
	self.Icon = "entities/npc_mossman.png"
	self.PosXOld = 0
	self.PosYOld = 0
	self.PosX = 0
	self.PosY = 0
	self.CanCast = true
	
end

function SPELL_ICON:SetColor(color)
	self.Color = color
end

function SPELL_ICON:CastSpell(simulated)
	if((LocalPlayer():Alive() and !self.OnCooldown and LocalPlayer().Resource >= SpellData[self.SpellID][11] or LocalPlayer():Alive() and !self.OnCooldown and self.Swapped) and self.CanCast and !LocalPlayer():IsFrozen())then
		if(targetEntity and SpellData[self.SpellID][7])then
			local LOS = ents.FindInCone( LocalPlayer():GetPos() - (LocalPlayer():GetAngles():Forward()*SpellData[self.SpellID][4]), LocalPlayer():GetAimVector(), 2048, 170 )
			local TargetInLOS = false
			local rootPos, rootAng = targetEntity:GetBonePosition(0) -- Find the position and angle of the target's root bone.
			local traceFrom = LocalPlayer():GetPos() + Vector(0, 0, 64)
			local vector = rootPos - traceFrom -- Use the position of our root bone to do our trace so that it hits.
			local eyeTrace = util.QuickTrace( traceFrom, (vector:GetNormal())*10000, LocalPlayer())
			
			if(targetEntity:IsPlayer() && targetEntity:Team() == LocalPlayer():Team() && SpellData[self.SpellID][8] != SPELLTYPE_UTILITY)then
				print("Friendly Fire")
				return true
			end
			
			if(eyeTrace.Entity != targetEntity && targetEntity != LocalPlayer())then
				print(eyeTrace.Entity)
				print("LOS")
				return true
			end
			
			for k, v in pairs(LOS) do
				if(v:EntIndex() == targetEntity:EntIndex())then
					TargetInLOS = true
					break
				end
			end
			
			if(!TargetInLOS && targetEntity != LocalPlayer())then
				print("LOS(out of view)")
				return true
			end
			
			if(((LocalPlayer():GetPos() - targetEntity:GetPos()):Length() >= SpellData[self.SpellID][4]))then
				print("RNG")
				return true
			end
			
			
		end

		self:DoGCD()
		
		if(!simulated)then
			runSpell(spellInfo[self.SpellID])
		end
		if(!SpellData[self.SpellID][9])then
			print(SpellData[self.SpellID][9])
			self.CoolDownProg = SpellData[self.SpellID][5]
			self.NextPress = CurTime() + self.CoolDown
			timer.Simple(self.CoolDown, function() if(self:IsValid())then self.CanCast = true end end)
		elseif(SpellData[self.SpellID][9] and !self.Swapped or SpellData[self.SpellID][9] and self.Icon == SpellData[self.SpellID][2])then
			self.CoolDownProg = SpellData[self.SpellID][5]
			self:SetIcon(SpellData[self.SpellID][10])
			self.Swapped = true
			self.NextPress = CurTime() + self.CoolDown
		elseif(SpellData[self.SpellID][9] and self.Swapped or SpellData[self.SpellID][9] and self.Icon == SpellData[self.SpellID][10])then
			self.CoolDownProg = SpellData[self.SpellID][5]
			self.NextPress = CurTime() + self.CoolDown
			self:SetIcon(SpellData[self.SpellID][2])
			self.Swapped = false
			timer.Simple(self.CoolDown, function() self.CanCast = true end)
		end
	else
		self:DoGCD()
		self.NextPress = CurTime() + 0.5
		surface.PlaySound("devin/blackbetty/weapons/CantCast.wav")
	end
end

function SPELL_ICON:OnCursorEntered()
end

function SPELL_ICON:Paint()
	surface.SetDrawColor(Color(0, 0, 0, 255))
	surface.DrawRect( 0, 0, 49*self.IconScale, 49*self.IconScale)
	surface.SetDrawColor( self.Color )
	surface.SetMaterial(Material( self.Icon or "entities/npc_mossman.png" ) )
	surface.DrawTexturedRect( 1, 1, 47*self.IconScale, 47*self.IconScale)
	
	if self.Selected then
		surface.SetDrawColor(Color(0, 255, 0, 40))
		surface.DrawRect( 0, 0, 49*self.IconScale, 49*self.IconScale)
		surface.SetDrawColor( self.Color )
	end
	
	if self.CoolDownProg > 0 then
		self.CoolDownProg = math.Clamp( self.CoolDownProg - FrameTime(), 0, self.CoolDown )
		surface.SetDrawColor( 10, 10, 10, 225 )
		surface.DrawRect( 1, 1, 47*self.IconScale, 47*self.IconScale * (self.CoolDownProg / self.CoolDown))
		self.OnCooldown = true
	else
		self.GCFSub = false
		self.OnCooldown = false
	end
	if(self.GCDProg > 0)then
		self.DoingGCD = true
		self.GCDProg = math.Clamp( self.GCDProg - FrameTime(), 0, self.GCD )
		surface.SetDrawColor( 10, 10, 10, 225 )
		surface.DrawRect( 1, 1, 47*self.IconScale, 47*self.IconScale * (self.GCDProg / self.GCD))
	else
		self.DoingGCD = false
	end
end

function SPELL_ICON:SetIcon(icon)
	self.Icon = icon
end

function SPELL_ICON:DoGCD()
	print(self.GCD)
	self.GCDProg = self.GCD
	self.CanCast = false
	timer.Simple(self.GCD, function()if(self:IsValid())then self.CanCast = true self.GCD = 0.5 end end)
end

function SPELL_ICON:SetCooldown(cd)
	self.CoolDown = cd
end

function SPELL_ICON:Think()	
	if(!self.InSpellbook)then
		if self.NextPress <= CurTime() and input.IsKeyDown( self.Key ) and self.CoolDownProg == 0 and !self.DoingGCD or self.NextPress <= CurTime() and input.IsKeyDown( self.Key ) and SpellData[self.SpellID][9] and !self.DoingGCD and self.CoolDownProg == 0 then
			if(((!CastBar or CastBar and self.CastWhileCasting) and (IsValid(targetEntity) or !self.RequiresTarget) and !self.DoingGCD or SpellData[self.SpellID][9] and !self.Swapped))then
				self:CastSpell()
				LocalPlayer().DoingGCD = true
				self.DoingGCD = true
				self.CanCast = false
			end
		end
		if !input.IsKeyDown( self.Key ) then
			self.Color = Color(255, 255, 255, 255)
		else
			self:SetColor(Color(255, 0, 0, 255))
		end
		if SpellData[self.SpellID][11] != nil and LocalPlayer().Resource < SpellData[self.SpellID][11] then
			self:SetColor(Color(100, 100, 255, 255))
		end
	end
end


vgui.Register( "SpellIcon", SPELL_ICON, "DImageButton")


